package dazuoye;

import java.io.*;
import java.util.*;
import java.text.*;


public class ScoreSystem  {

	private static BufferedReader  stdIn =
		new  BufferedReader(new  InputStreamReader(System.in));
	private static PrintWriter  stdOut = new  PrintWriter(System.out, true);
	private static PrintWriter  stdErr = new  PrintWriter(System.err, true);
	
	private ScoreCatalog  scorecatalog;
	private ScoreSummary  scoresummary;

	private SSFormatter ssFormatter;

	
	public static void  main(String[]  args) throws Exception  {
		ScoreCatalog scorecatalog = null;

			try {
				scorecatalog =
					(new FileSCatalogLoader()).loadSCatalog("scorecatalog.dat");
			} catch (FileNotFoundException fnfe) {
				stdErr.println("The file does not exist");

				System.exit(1);

			} catch (DataFormatException dfe) {
				stdErr.println("The file contains malformed data: "
				               + dfe.getMessage());

				System.exit(1);
			}

			ScoreSystem  application =
				new ScoreSystem(scorecatalog);
			application.run();

	}

	
	private ScoreSystem(ScoreCatalog initialCatalog) {

		this.scorecatalog = initialCatalog;
		this.scoresummary = new ScoreSummary();
		this.ssFormatter =
			PlainTextSSFormatter.getSingletonInstance();

		loadScoreSummary();
	}

	
	private void loadScoreSummary() {
        
		ScoreReport scorereport1 =new ScoreReport();
		ExamScore examscore11 =this.scorecatalog.getExamScore("Ugaoshu030103");
		ExamScore examscore12 =this.scorecatalog.getExamScore("Ulisan030103");
		ExamScore examscore13 =this.scorecatalog.getExamScore("Ujava030103");
		ExamScore examscore14 =this.scorecatalog.getExamScore("Uyingyu030103");
		if(examscore11!=null&&examscore12!=null&&examscore13!=null&&examscore14!=null){
			scorereport1.addFScore(new FinalScore(examscore11,80));
			scorereport1.addFScore(new FinalScore(examscore12,75));
			scorereport1.addFScore(new FinalScore(examscore13,90));
			scorereport1.addFScore(new FinalScore(examscore14,95));
			this.scoresummary.addScoreReport(scorereport1);
		}
		
		ScoreReport scorereport2 =new ScoreReport();
		ExamScore examscore21 =this.scorecatalog.getExamScore("Ugaoshu030209");
		ExamScore examscore22 =this.scorecatalog.getExamScore("Ulisan030209");
		ExamScore examscore23 =this.scorecatalog.getExamScore("Ujava030209");
		ExamScore examscore24 =this.scorecatalog.getExamScore("Uyingyu030209");
		if(examscore21!=null&&examscore22!=null&&examscore23!=null&&examscore24!=null) {
			scorereport2.addFScore(new FinalScore(examscore21,40));
			scorereport2.addFScore(new FinalScore(examscore22,85));
			scorereport2.addFScore(new FinalScore(examscore23,70));
			scorereport2.addFScore(new FinalScore(examscore24,75));
			this.scoresummary.addScoreReport(scorereport2);
		}
		
		ScoreReport scorereport3 =new ScoreReport();
		ExamScore examscore31 =this.scorecatalog.getExamScore("Ggaoshu020701");
		ExamScore examscore32 =this.scorecatalog.getExamScore("GC++020701");
		ExamScore examscore33 =this.scorecatalog.getExamScore("GPython020701");
		
		if(examscore31!=null&&examscore32!=null&&examscore33!=null) {
			scorereport3.addFScore(new FinalScore(examscore31,63));
			scorereport3.addFScore(new FinalScore(examscore32,72));
			scorereport3.addFScore(new FinalScore(examscore33,83));
			
			this.scoresummary.addScoreReport(scorereport3);
		}

	}

	
	private void run() throws Exception  {

		int  choice = getChoice();

		while (true)  {
			if (choice == 1)  {
				displayCatalog();
			} else if (choice == 2)  {
				this.ssFormatter =
					PlainTextSSFormatter.getSingletonInstance();
				writeFile(
					readFilename(),
					this.ssFormatter.formatSS(this.scoresummary));
				    stdOut.println("Plain Text is written successfully!");
				    stdOut.println(" ");
			} else if (choice == 3)  {
				this.ssFormatter =
					HTMLSSFormatter.getSingletonInstance();
				writeFile(
					readFilename(),
					this.ssFormatter.formatSS(this.scoresummary));
				    stdOut.println("HTML is written successfully!");
				    stdOut.println(" ");
			} else if (choice == 4)  {
				displayExamScore();
				
			} else if (choice == 5)  {
				AddExamScore();
				
			} else if (choice == 0)  {
				System.exit(1);
				
			} 

			choice = getChoice();
		}
	}

	
	


	private int  getChoice() throws IOException, Exception  {

		int  input;

		do  {
			try  {
				
				
				for(int a=0;a<10;a++) {
					Thread.sleep(50);
				}
				
				stdErr.println();
				stdErr.print("[0]  Quit\n"
				             + "[1]  Display ScoreCatalog\n"
				             + "[2]  ScoreSummary scoresummary (Plain Text)\n"
				             + "[3]  ScoreSummary scoresummary (HTML)\n" 
				             + "[4]  Display ExamScore\n" 
				             + "[5]  Add ExamScore\n" 
				             + "choice> \n");
				stdErr.flush();
                
				input = Integer.parseInt(stdIn.readLine());

				stdErr.println();

				if (0 <= input && 5 >= input)  {
					break;
				} else {
					stdErr.println("Invalid choice:  " + input);
				}
			} catch (NumberFormatException  nfe)  {
				stdErr.println(nfe);
			}
		}  while (true);

		return  input;
	}

	
	private void displayCatalog() {

		int size = this.scorecatalog.getNumberOfExamScores();

		if (size == 0) {
			stdErr.println("The scorecatalog is empty");
		} else {
			for (ExamScore examscore : this.scorecatalog) {
				stdOut.println(examscore.getCode() + " "
				               + examscore.getSubject()+ " "+ examscore.getScore()+ " "+ examscore.getName());
			}
		}
	}

	private void displayExamScore() throws IOException {
		
		String a=readCode();
		int size = this.scorecatalog.getNumberOfExamScores();
        boolean flag=true;
		if (size == 0) {
			stdErr.println("The scorecatalog is empty");
		} else {
			for (ExamScore examscore : this.scorecatalog) {
				if(examscore.getCode().equals(a)) {
				
				stdOut.println("          ");
				stdOut.println("Code: " + examscore.getCode()+"\n"+
				"Subject: " + examscore.getSubject()+"\n"+
				"Score: " + examscore.getScore()+"\n"+
				"Name: " + examscore.getName()+"\n");
				flag=false;
				}
			}
			if(flag==true) {
				stdErr.println("The Code does not exist");
			}
		}
	}
	
	private void AddExamScore() throws Exception {

		String stu=readStu();
		String code=readCode();
		String subject=readSubject();
		String score=readScore();
		String name=readName();
		String teacher=readTeacher();
		String sex=readSex();
		String idnumber=readIdnumber();
		String major=readMajor();
		
		File file=new File("scorecatalog.dat");
		try {
			FileWriter fw=new FileWriter(file,true);
			BufferedWriter bw=new BufferedWriter(fw);
			bw.write("\n");
			bw.flush();
			bw.write(stu+"_"+code+"_"+subject+"_"+score+"_"+name+"_"+teacher+"_"+sex+"_"+idnumber+"_"+major);
			bw.flush();
			bw.close();
			fw.close();
		} catch (IOException e1) {
			
			e1.printStackTrace();
		}
		
		
		scorecatalog = (new FileSCatalogLoader()).loadSCatalog("scorecatalog.dat");
		ScoreSystem  application = new ScoreSystem(scorecatalog);
			application.run();
		
	}
	
	private void writeFile(String filename, String content)
		throws IOException {

		
        File file=new File(filename);
        if(file.exists()){
            file.delete();
        }
        file.createNewFile();
        FileWriter filewriter=new FileWriter(filename,true);
        BufferedWriter filewriter1=new BufferedWriter(filewriter);
        filewriter1.write(content);
        filewriter1.close();
        filewriter.close();
	}
	

	
	private  String readFilename() throws IOException  {

		stdErr.print("Filename> \n");
		stdErr.flush();

		return stdIn.readLine();
	}
	
	private  String readCode() throws IOException  {

		stdErr.print("Code> \n");
		stdErr.flush();

		return stdIn.readLine();
	}
	
	private  String readSubject() throws IOException  {

		stdErr.print("Subject> \n");
		stdErr.flush();

		return stdIn.readLine();
	}
	
	private  String readScore() throws IOException  {

		stdErr.print("Score> \n");
		stdErr.flush();

		return stdIn.readLine();
	}
	
	private  String readName() throws IOException  {

		stdErr.print("Name> \n");
		stdErr.flush();

		return stdIn.readLine();
	}
	
	private  String readTeacher() throws IOException  {

		stdErr.print("Teacher> \n");
		stdErr.flush();

		return stdIn.readLine();
	}
	
	private  String readSex() throws IOException  {

		stdErr.print("Sex> \n");
		stdErr.flush();

		return stdIn.readLine();
	}
	
	private  String readIdnumber() throws IOException  {

		stdErr.print("Idnumber> \n");
		stdErr.flush();

		return stdIn.readLine();
	}
	
	private  String readMajor() throws IOException  {

		stdErr.print("Major> \n");
		stdErr.flush();

		return stdIn.readLine();
	}
	
	private  String readStu() throws IOException  {

		stdErr.print("University Student or Graduate Student> \n");
		stdErr.flush();

		return stdIn.readLine();
	}
}